package edu.lit.B180401.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import edu.lit.B180401.DBHelper;
import edu.lit.B180401.DBUtil;
import edu.lit.B180401.HobbyInfo;
import edu.lit.B180401.HobbyType;
import edu.lit.B180401.Student;

public class Service {

    SQLiteDatabase db;

    //根据爱好名称查找爱好
    public HobbyType selectHobbyByName(Context context,String name){
        HobbyType hobbyType=new HobbyType();
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_HOBBYTYPE, null, DBUtil.HOBBYTYPE_NAME+"=?", new String[]{name+""},null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                hobbyType.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYTYPE_ID)));
                hobbyType.setName(cursor.getString(cursor.getColumnIndex(DBUtil.HOBBYTYPE_NAME)));
            }
        }
        return  hobbyType;
    }
    //根据学号和姓名查找学生
    public Student selectStudentByStunoAndName(Context context,String stuno,String Name){
        Student student=new Student();
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_STUINFO, null, DBUtil.STUINFO_NAME+"=? and "+DBUtil.STUINFO_STUNO+"=? ", new String[]{Name+"",stuno},null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                student.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.STUINFO_ID)));
                student.setName(cursor.getString(cursor.getColumnIndex(DBUtil.STUINFO_NAME)));
                student.setStuno(cursor.getString(cursor.getColumnIndex(DBUtil.STUINFO_STUNO)));
            }
        }
        return  student;
    }
    //向“hobbyinfo”中插入数据
    public int insertHobbyInfo(Context context,int stuid,int hobbytypeid){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db=helper.getWritableDatabase();
        values.put(DBUtil.HOBBYINFO_STUID,stuid);
        values.put(DBUtil.HOBBYINFO_HOBBYTYPEID,hobbytypeid);
        long number=db.insert(DBUtil.TABLE_HOBBYINFO,null,values);
        return (int)number;
    }
    //向“stuinfo”中插入数据
    public int insertStuInfo(Context context,String name,String stuno){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db=helper.getWritableDatabase();
        values.put(DBUtil.STUINFO_NAME,name);
        values.put(DBUtil.STUINFO_STUNO,stuno);
        long number=db.insert(DBUtil.TABLE_STUINFO,null,values);
        return (int)number;
    }
    //检索所有“hobbyinfo”表数据
    public List<HobbyInfo> selectHobbtInfo(Context context){
        List<HobbyInfo> list=new LinkedList<HobbyInfo>();
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_HOBBYTYPE, null, null, null,null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                HobbyInfo hobbyInfo=new HobbyInfo();
                hobbyInfo.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYINFO_ID)));
                hobbyInfo.setStuid(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYINFO_STUID)));
                hobbyInfo.setHobbytypeid(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYINFO_HOBBYTYPEID)));
                list.add(hobbyInfo);
            }
        }
        return  list;
    }

    //根据名字查找hobbytytpe
    public  HobbyType selcetHobbyTypeByName(Context context,String name){
        HobbyType hobbyType=new HobbyType();
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_HOBBYTYPE, null, DBUtil.HOBBYTYPE_NAME+"=?  ", new String[]{name+""},null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                hobbyType.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYTYPE_ID)));
                hobbyType.setName(cursor.getString(cursor.getColumnIndex(DBUtil.HOBBYTYPE_NAME)));
            }
        }
        return  hobbyType;

    }

    //删除所有指定stuid的habbyinfo
    public int delByStuid(Context context,int stuid){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getWritableDatabase();
        int number = db.delete(DBUtil.TABLE_HOBBYINFO, DBUtil.HOBBYINFO_STUID+"=?", new String[]{stuid+""});
        return  number;
    }

    //向“hobbytype”中插入数据
    public int insertHobbyType(Context context,String name){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db=helper.getWritableDatabase();
        values.put(DBUtil.HOBBYTYPE_NAME,name);
        long number=db.insert(DBUtil.TABLE_HOBBYTYPE,null,values);
        return (int)number;
    }

    //查找学生
    public List<Student> selectStudent(Context context){
        List<Student> list=new LinkedList<Student>();

        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_STUINFO, null, null, null,null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                Student student=new Student();
                student.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.STUINFO_ID)));
                student.setName(cursor.getString(cursor.getColumnIndex(DBUtil.STUINFO_NAME)));
                student.setStuno(cursor.getString(cursor.getColumnIndex(DBUtil.STUINFO_STUNO)));
                list.add(student);
            }
        }
        return  list;
    }


    //检索学生id“hobbyinfo”表数据
    public List<HobbyInfo> selectHobbtInfoByStuid(Context context,Integer stuid){
        List<HobbyInfo> list=new LinkedList<HobbyInfo>();
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_HOBBYINFO, null, DBUtil.HOBBYINFO_STUID+"=?", new String[]{stuid+""},null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                HobbyInfo hobbyInfo=new HobbyInfo();
                hobbyInfo.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYINFO_ID)));
                hobbyInfo.setStuid(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYINFO_STUID)));
                hobbyInfo.setHobbytypeid(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYINFO_HOBBYTYPEID)));
                System.out.println("查找hobbyInfo："+hobbyInfo);

                list.add(hobbyInfo);
            }
        }
        return  list;
    }


    //根据id查找hobbytytpe
    public  HobbyType selcetHobbyTypeById(Context context,int id){
        HobbyType hobbyType=new HobbyType();
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_HOBBYTYPE, null, DBUtil.HOBBYTYPE_ID+"=?  ", new String[]{id+""},null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                hobbyType.setId(cursor.getInt(cursor.getColumnIndex(DBUtil.HOBBYTYPE_ID)));
                hobbyType.setName(cursor.getString(cursor.getColumnIndex(DBUtil.HOBBYTYPE_NAME)));
                System.out.println("查找HobbyType："+hobbyType);
            }
        }
        return  hobbyType;

    }
}
